<template>
  <!-- 党群活动 -->
  <base-breadcrumb class="partyActivity-container">
    <searchData
      :search-data="searchArr"
      @search="handleSearch"
    />
    <a-card :bordered="false">
      <!-- <title-name title="党群活动列表" /> -->
      <div class="pub-table-action-btn">
        <a-button
          v-if="isPartyManager"
          v-btnPermission="'pa_009_add_01'"
          type="primary"
          @click="handleUpdate({}, 'add')"
        >
          新增
        </a-button>
      </div>
      <div
        class="pub-table-select-count"
        style="margin: 10px 0"
      >
        <a-icon
          type="info-circle"
          theme="twoTone"
        />
        <span>
          共计：
          <a>{{ totalInfo.totalNum || 0 }} 个</a>
        </span>
      </div>
      <list-table
        ref="table"
        row-key="id"
        :columns="columns"
        :request="loadData"
        show-size-changer
        show-quick-jumper
        show-pagination="auto"
      >
        <template
          slot="activityLevel"
          slot-scope="text, record"
        >
          {{ filterActivityLevel(text) }}
        </template>
        <template
          slot="ifPushNews"
          slot-scope="text, record"
        >
          {{ text === 1 ? '是' : '否' }}
        </template>
        <template
          slot="status"
          slot-scope="text, record"
        >
          {{ filterStatus(text) }}
        </template>
        <template
          slot="action"
          slot-scope="text, record"
        >
          <MoreButton>
            <a
              v-btnPermission="'pa_009_loo_04'"
              @click="handleUpdate(record, 'look')"
            >查看</a>
            <a
              v-if="record.updateAndDelete"
              v-btnPermission="'pa_009_edi_02'"
              @click="handleUpdate(record, 'edit')"
            >编辑</a>
            <!-- <span v-btnPermission="'pa_009_pus_05'" v-if="record.status && record.status == 5">推送</span>
            <span v-btnPermission="'pa_009_rev_06'" v-if="record.status && record.status == 6">撤销</span> -->
            <!-- 状态为审核中、审核不通过、可报道、正报道 且 不是（党委举办，选择需要推为新闻报道）的情况下显示审批信息按钮 -->
            <a
              v-if="(record.status == 2 || record.status == 4 || record.status == 5 || record.status == 6) && (record.activityLevel != 1 || record.ifPushNews != 1)"
              v-btnPermission="'pa_009_moi_07'"
              @click="handleUpdate(record, 'auditInfo')"
            >审批信息</a>
            <a
              v-if="record.status == 0 && record.updateAndDelete"
              v-btnPermission="'pa_009_del_03'"
              @click="handleUpdate(record, 'delete')"
            >删除</a>
          </MoreButton>
        </template>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
import { STable } from '@/components' // 表格
import { getActivityList, deleteActivity } from '@/api/partyMasses/partyManage/partyActivity.js'
import { packageConfirm } from '@/utils/baseAction.js'
import Selector from '@/components/Select/Selector.vue'
import { STATUS_MAP_NEWS, PARTY_TYPE_MAP } from '@/config/businessConfig.js'
import { Collapse } from 'ant-design-vue'
import { getCurrentPartyLevel } from '@/api/partyMasses'

const searchArr = [
  {
    name: '活动名称',
    value: 'activityName',
    type: 'input'
  },
  {
    name: '活动级别',
    value: 'activityLevel',
    type: 'select',
    data: Object.keys(PARTY_TYPE_MAP).map(key => {
      return {
        value: key,
        label: PARTY_TYPE_MAP[key]
      }
    })
  },
  {
    name: '举行组织名称',
    value: 'partyOrgName',
    type: 'input'
  },
  {
    name: '活动时间',
    value: 'activityTime',
    type: 'timeRange',
    attr: {
      rangeFormat: ['activityStartTime', 'activityEndTime']
    }
  },
  {
    name: '状态',
    value: 'status',
    type: 'select',
    data: Object.keys(STATUS_MAP_NEWS).map((key) => {
      return {
        value: key,
        label: STATUS_MAP_NEWS[key].label
      }
    })
  }
]
const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'index',
    customRender() {
      return arguments[2] + 1
    }
  },
  {
    title: '活动名称',
    dataIndex: 'activityName'
  },
  {
    title: '活动类型',
    dataIndex: 'activityTypeName'
  },
  {
    title: '举办组织',
    dataIndex: 'partyOrgName',
    ellipsis: true
  },
  {
    title: '活动级别',
    dataIndex: 'activityLevel',
    scopedSlots: { customRender: 'activityLevel' }
  },
  {
    title: '是否推为新闻报道',
    dataIndex: 'ifPushNews',
    scopedSlots: { customRender: 'ifPushNews' }
  },
  {
    title: '活动日期',
    dataIndex: 'activityTime'
  },
  {
    title: '创建时间',
    dataIndex: 'createTime'
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '180px',
    align: 'center',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name:'a'+ Date.now(),
  components: {
    STable,
  },
  data() {
    return {
      totalInfo: {
        totalNum: 0
      },
      searchArr,
      columns: columns,
      queryParam: {},
      isPartyManager: false // 是否为党组织管理员
    }
  },
  created() {
    this.getCurrentPartyLevelReq()
  },
  methods: {
    /**
     * 获取当前登录人的党组织管理等级
     * 如果返回值中有数组不为空，则是管理人员
     */
    async getCurrentPartyLevelReq() {
      try {
        const res = await getCurrentPartyLevel()
        this.isPartyManager = Object.keys(res.data).some((key) => res.data[key] && res.data[key].length)
      } catch (e) {
        console.error(e)
      }
    },
    filterActivityLevel(level) {
      return PARTY_TYPE_MAP[level] || ''
    },
    filterStatus(status) {
      return STATUS_MAP_NEWS[status]?.label
    },
    /**
     * 处理数据的相关操作（增删改查）
     */
    handleUpdate(data, pageType) {
      const commonHandler = () => {
        this.$router.push({
          path: '/partyMasses/partyManage/partyActivity/update',
          query: {
            id: data.id || '',
            pageType: pageType,
            status: data.status || '0',
          }
        })
      }
      const actionMap = {
        add: commonHandler,
        edit: commonHandler,
        look: commonHandler,
        auditInfo: () => {
          this.$bus.$emit('showApprovalDetailsDialog', data.id)
        },
        delete: async () => {
          packageConfirm({
            callback: async () => {
              const res = await deleteActivity({ id: data.id })
              this.$message.success('删除成功')
              this.refreshTable()
            }
          })
        }
      }
      actionMap[pageType] && actionMap[pageType]()
    },
    handleSearch(val = {}) {
      this.queryParam = {
        ...val
      }
      this.refreshTable()
    },
    refreshTable() {
      this.$refs.table.refresh(true)
    },
    async loadData(parameter) {
      const requestParameters = Object.assign({}, { ...parameter }, { query: { ...this.queryParam } })
      return getActivityList(requestParameters).then((res) => {
        this.totalInfo.totalNum = res.totalSize
        return {
          code:200,
          data: res.data,
          currentPage: res.currentPage,
          totalSize: res.totalSize
        }
      })
    }
  }
}
</script>

<style>
</style>